/**
 * Created by PengWei on 2017/10/12.
 */
namespace game {
    import GTextField = fairygui.GTextField;
    export class TextUtil {
        public constructor() {
        }

        public static FormatNumTxt(num:number)
        {
            if(num>100000000){
                return (num / 100000000).toFixed(1) + "亿";
            }else if(num>10000000){
                return (num / 10000000).toFixed(1) + "千万";
            }else if(num>=10000){
                if(num % 10000 >0)
                    return (num/10000).toFixed(1) + "万";
                return Math.floor(num/10000) + "万";
            }
            return num.toString();
        }

        public static Format(format:string,...params)
        {
            let replace = format;
                for(let i = 0;i<params.length;i++)
                    replace = replace.replace("{"+i+"}",params[i]);
            return replace;
        }

        public static FormatMinuteTime(sec:number)
        {
            let tSec = sec<0?0:sec;
            let minutes = Math.floor(tSec / (60));
            tSec -= minutes * (60);
            let returnTxt = "";
            returnTxt += minutes<10?"0" + minutes:minutes.toString();
            returnTxt += ":";
            returnTxt += tSec<10?"0"+tSec:tSec.toString();
            return returnTxt;
        }

        public static FormatHourTime(sec:number,ignoreZeroHour:boolean = false)
        {
            let tSec = sec;
            let hours = Math.floor(tSec/(60 * 60));
            tSec -= hours * (60 * 60);

            let minutes = Math.floor(tSec / (60));
            tSec -= minutes * (60);

            let returnTxt = "";
            if(hours > 0 || ignoreZeroHour == false)
            {
                returnTxt += hours<10?"0"+hours:hours.toString();
                returnTxt += ":";
            }
            returnTxt += minutes<10?"0" + minutes:minutes.toString();
            returnTxt += ":";
            returnTxt += tSec<10?"0"+tSec:tSec.toString();
            return returnTxt;
        }

        public static ClearParentheses(txt:string){
            let reg:RegExp = /\([^\(\)]+\)/g;
            let reg2:RegExp = /（[^（）]+）/g;
            let matchResult = txt.match(reg);
            if(matchResult)
                matchResult = matchResult.concat(txt.match(reg2));
            let returnTxt = txt;
            if(matchResult)
            {
                for(let i = 0;i<matchResult.length;i++)
                    returnTxt = returnTxt.replace(matchResult[i],"");
            }

            return returnTxt;
        }

        public static PackageUbbWithColor(txt: string, color: number) {
            return "[color=#" + color.toString(16) + "]" + txt + "[/color]";
        }

        public static PackageUbbWithColorAndSize(txt: string, color: number,size:number) {
            return "[size="+size+"][color=#" + color.toString(16) + "]" + txt + "[/color][/size]";
        }

        public static PackageUbbWithSize(txt:string,size:number){
            return "[size="+size+"]" + txt + "[/size]";
        }

        public static GetUpArrow() {
            // return this.PackageUbbWithColor("↑", Color.QUALITY_1);
        }

        public static GetDownArrow() {
            // return this.PackageUbbWithColor("↓", Color.COLOR_RED);
        }

        public static PackageProcess(cur:number,total:number,includeTotalb:boolean = true,showRed:boolean = false)
        {
            // if(cur>=total)
            //     return (showRed?cur.toString():this.PackageUbbWithColor(cur.toString(),Color.TXT_GREEN)) + (includeTotalb?("/" + total):"");
            // if(showRed)
            //     return this.PackageUbbWithColor(cur.toString(),Color.MSG_RED) + (includeTotalb?("/" + total):"");
            // return cur + (includeTotalb?("/" + total):"");
        }

        public static getRandomColor(r?:number,g?:number,b?:number)
        {
            let rC = r>=0?r:Math.floor(Math.random() * (0xFF+1));
            let rG = g>=0?g:Math.floor(Math.random() * (0xFF+1));
            let rB = b>=0?b:Math.floor(Math.random() * (0xFF+1));
            return rC<<16|rG<<8|rB;
        }
        
        public static getFoodRankDesc(rank)
        {
            switch(rank)
            {
                case 1:
                    return '肥沃';
                case 2:
                    return '一般';
                case 3:
                    return '贫瘠';
                case 4:
                    return '荒芜';
            }
        }

        /**
         * 图文混排
         */
        public static richText(tf:GTextField,txt:string,...imgAndIdxList)
        {
            let h = tf.height;
            tf.text = txt;

            let offsetX = 0;
            let offsetY = 0;

            for(let i = 0;i<imgAndIdxList.length;i+=2)
            {
                let idx = imgAndIdxList[i];
                let img = imgAndIdxList[i+1];

                let insertContent = "[size="+img.width+"] [/size]";
                txt = txt.substring(0,idx) + insertContent + txt.substring(idx+1);
                tf.text = txt;

                if(tf["_textField"].textHeight != h &&   tf["_textField"].textHeight - (img.width - tf["_textField"].size) != h)
                {
                    offsetX -= tf["_textField"].textWidth;
                    offsetY = h;
                }

                let tx = tf.x + tf["_textField"].size * idx + offsetX;
                let ty = tf.y + offsetY;

                offsetX += img.width - tf["_textField"].size;

                img.x = tx;
                img.y = ty;
            }
        }

        public static GetChineseNumTxt(num:number)
        {
            let numList :string[] = ['','一','二','三','四','五','六','七','八','九','十'];
            let tenNum: number = Math.floor(num / 10);
            let extraNum: number = num - tenNum * 10;
            let nameValue = '';
            if (tenNum <= 0) {
                nameValue = numList[extraNum];
            } else if (tenNum == 1) {
                nameValue = numList[10] + numList[extraNum];
            } else {
                nameValue = numList[tenNum] + numList[10] + numList[extraNum];
            }
            return nameValue;
        }



    }
    export class EffectUtil{
        public static gray = [
            0.01, 0, 0, 0.2, 0,
            0, 0.01, 0, 0.2, 0,
            0, 0, 0.01, 0.2, 0,
            0, 0, 0, 1, 0
        ]

        public static gray2 = [
            0.1, 0, 0, 0, 0,
            0, 0.1, 0, 0, 0,
            0, 0, 0.1, 0, 0,
            0, 0, 0, 1, 0
        ]

        public static gray3 = [
            0.1, 0, 0, 0.2, 0,
            0, 0.1, 0, 0.2, 0,
            0, 0, 0.1, 0.2, 0,
            0, 0, 0, 1, 0
        ]
    }

}